// WTM默认页面 Wtm buidin page

using System;
using System.Linq;
using HengTong.Model.Db;
using HengTong.Model.Db.Hr;
using HengTong.Model.Db.Hr.Bill;
using HengTong.Model.Db.System;
using Microsoft.EntityFrameworkCore;
using NPOI.HSSF.Util;
using TiaoTaoHelper.Wtm.Models;
using WalkingTec.Mvvm.Core;

namespace HengTong.ViewModel._Admin.FrameworkGroupVMs
{
    public class FrameworkGroupBatchVm : BaseBatchVM<FrameworkGroup, BaseVM>
    {
        public FrameworkGroupBatchVm()
        {
            ListVM = new FrameworkGroupListVm();
        }


        public override bool DoBatchDelete()
        {
            var transaction = DC.Database.BeginTransaction();
            try
            {
                var ids = Ids.Where(e => Guid.TryParse(e, out _)).Select(Guid.Parse).ToList();
                if (ids.Any())
                {
                    //FK_SysJobPositionRole_SysJobPosition_JobPositionId
                    //FK_SysUserJobPosition_SysJobPosition_JobPositionId
                    //FK_SysHrStaffNeedRequestBillEntry_SysJobPosition_JobPositionId
                    //FK_SysPerson_SysJobPosition_JobPositionId
                    //FK_SysHrStaffPool_SysHrStaffNeedRequestBillEntry_StaffNeedEntryId
                    //FK_SysGroupOrg_FrameworkGroups_GroupId
                    DC.Set<SysPerson>()
                        .Where(e => ids.Contains(e.JobPosition.GroupId.Value))
                        .ExecuteDelete();
                    DC.Set<SysPerson>()
                        .Where(e => ids.Contains(e.GroupId.Value))
                        .ExecuteDelete();
                    DC.Set<SysHrStaffPool>()
                        .Where(e => ids.Contains(e.StaffNeedEntry.JobPosition.GroupId.Value))
                        .ExecuteDelete();
                    DC.Set<SysHrStaffNeedRequestBillEntry>()
                        .Where(e => ids.Contains(e.JobPosition.GroupId.Value))
                        .ExecuteDelete();
                    DC.Set<SysUserJobPosition>()
                        .Where(e => ids.Contains(e.JobPosition.GroupId.Value))
                        .ExecuteDelete();
                    DC.Set<SysJobPositionRole>()
                        .Where(e => ids.Contains(e.JobPosition.GroupId.Value))
                        .ExecuteDelete();
                    DC.Set<SysJobPositionOrg>()
                        .Where(e => ids.Contains(e.JobPosition.GroupId.Value))
                        .ExecuteDelete();
                    DC.Set<SysJobPosition>()
                        .Where(e => ids.Contains(e.GroupId.Value))
                        .ExecuteDelete();
                    DC.Set<SysGroupOrg>()
                        .Where(e => ids.Contains(e.GroupId.Value))
                        .ExecuteDelete();
                }

                if (base.DoBatchDelete())
                {
                    this.Ids = ids.Select(x => x.ToString()).ToArray();
                    transaction.Commit();
                    return true;
                }
                else
                {
                    transaction.Rollback();
                    return false;
                }
            }
            catch (Exception e)
            {
                transaction.Rollback();
                MSD.AddModelError(" ", "数据删除失败，请检查是否有相关联的数据！");
                return false;
            }
        }
    }
}
